//
//  PywGetSmsCodeByUsernameApi.m
//  PywSdk
//
//  Created by smallwave on 16/10/11.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywGetSmsCodeByUsernameApi.h"
#import "AIFGetSmsCodeByUsernameManager.h"
#import "PywResourceUtils.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PropertyListReformer.h"

@interface PywGetSmsCodeByUsernameApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywGetSmsCodeByUsernameApi

- (void)requestGetSmsCodeByUsernameWithParams:(MutableDictionary *)params
                                      success:(Successd)success
                                         fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    [PywMBProgressHUD showMessage:@"获取验证码, 请稍后..."];
    AIFGetSmsCodeByUsernameManager *manager = [AIFGetSmsCodeByUsernameManager manager];
    manager.delegate = self;
    manager.username = [params objectForKey:@"username"];
    manager.smsType = [params objectForKey:@"smstype"];
    [manager loadData];
    
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUD];
    [self getSmsCodeByUsernameSuccess:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD hideHUD];
    [self getSmsCodeByUsernameFail:0 msg:@"似乎已断开与互联网的连接?"];
}
/**
 *  获取验证码成功
 */
- (void)getSmsCodeByUsernameSuccess:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    NSString *msg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
    if (resultCode == 200) {
        NSDictionary *data = [dict objectForKey:@"data"];
        if (self.successd) {
            self.successd(nil,data);
        }
    }else {
        [self getSmsCodeByUsernameFail:resultCode msg:msg];
    }
}
/**
 *  获取验证码失败
 */
- (void)getSmsCodeByUsernameFail:(NSInteger)resultCode
                             msg:(NSString *)msg
{
    [PywMBProgressHUD showError:msg];
    if (self.failure) {
        self.failure([self errorWithCode:resultCode]);
    }
}


@end
